package com.youlian.cloud.service.impl;

import com.youlian.cloud.core.exception.BusinessException;
import com.youlian.cloud.core.util.StringUtils;
import com.youlian.cloud.dao.IBaseDaoSupport;
import com.youlian.cloud.core.bean.PageModel;
import com.youlian.cloud.service.ICommonService;
import com.youlian.cloud.spring.common.utils.BeanUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通用的业务层增删改查方法
 *
 * @author : liukx
 * @create : 2018/5/17 10:02
 * @email : liukx@elab-plus.com
 */
public abstract class CommonServiceAdaptor<T> implements ICommonService<T> {
    /**
     * 客户端传递的dao具体实现
     * @return
     */
    protected abstract IBaseDaoSupport<T> getBaseDaoSupport();

    private String defaultParamsError = "CORE.PARAMS.001";
    private String defaultParamsMsg = "必填参数不能为空";

    /**
     * 通用的添加内容
     *
     * @param obj 对象内容 必须是构造参数
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(T obj) throws Exception {
        int id = getBaseDaoSupport().insert(obj);
        return id;
    }

    /**
     * 通用的业务层根据主键修改编号
     *
     * @param obj 里面必须包含主键id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(T obj) throws Exception {
        Object id = BeanUtils.forceGetProperty(obj, "id");
        if (id == null) {
            throw new BusinessException(defaultParamsError, defaultParamsMsg);
        }
        return getBaseDaoSupport().updateById(obj);
    }

    /**
     * 根据entity查询一个对象
     *
     * @param obj entity映射对象
     * @return
     * @throws Exception
     */
    @Override
    public T selectByObject(T obj) throws Exception {
        return getBaseDaoSupport().selectByObject(obj);
    }

    /**
     * 根据主键查询对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public T selectById(String id) throws Exception {
        if (id == null || StringUtils.isEmpty(id)) {
            throw new BusinessException(defaultParamsError, defaultParamsMsg);
        }
        return getBaseDaoSupport().selectById(id);
    }

    /**
     * 根据entity属性内容查询一个集合对象
     *
     * @param obj
     * @return
     * @throws Exception
     */
    @Override
    public List<T> selectByList(T obj) throws Exception {
        return getBaseDaoSupport().selectByList(obj);
    }

    /**
     * 根据实体参数查询对象
     *
     * @param pageModel 分页参数
     * @param o         查询字段
     * @return
     * @throws Exception
     */
    @Override
    public PageModel<T> selectByPage(PageModel pageModel, T o) throws Exception {
        return getBaseDaoSupport().selectByPage(pageModel, o);
    }

    /**
     * 查询一个集合对象
     *
     * @param obj       entity对象
     * @param rowMapper 实现了RowMapper接口的对象,参数映射会回调该类的方法
     * @return
     * @throws Exception
     */
    @Override
    public List<T> selectByMapper(T obj, RowMapper<T> rowMapper) throws Exception {
        return getBaseDaoSupport().selectByMapper(obj, rowMapper);
    }
}
